﻿using MediatR;
using Production_Get.API.Infrastructures.Interfaces;
using Return.Enum;
using THE_WIND_RISES.Common.Domans.BasicInformation.MainPlans;
using THE_WIND_RISES.Common.Domans.Produce.DemandPlanning;
using THE_WIND_RISES.Common.Dtos;

namespace Production_Get.API.Application.Commands.ReportForm
{
    /// <summary>
    /// 工单执行进度看板显示
    /// </summary>
    public class WorkOrderBoardShowHandler : IRequestHandler<WorkOrderBoardShowCommand, Result>
    {

        private readonly IBaseRepositoryGet<ProductionTasks> productionTasksRepository;
        private readonly IBaseRepositoryGet<Mainpalan> mainpalanRepository;
        private readonly IBaseRepositoryGet<WorkOrderBoards> workOrderBoardsRepository;
        private readonly IBaseRepositoryGet<Mainplandetail> mainplandetailRepository;
        private readonly ILogger<WorkOrderBoardShowHandler> logger;

        public WorkOrderBoardShowHandler(IBaseRepositoryGet<ProductionTasks> productionTasksRepository, IBaseRepositoryGet<WorkOrderBoards> workOrderBoardsRepository, ILogger<WorkOrderBoardShowHandler> logger, IBaseRepositoryGet<Mainpalan> mainpalanRepository, IBaseRepositoryGet<Mainplandetail> mainplandetailRepository)
        {
            this.productionTasksRepository = productionTasksRepository;
            this.mainpalanRepository = mainpalanRepository;
            this.workOrderBoardsRepository = workOrderBoardsRepository;
            this.logger = logger;
            this.mainplandetailRepository = mainplandetailRepository;
        }

        public async Task<Result> Handle(WorkOrderBoardShowCommand request, CancellationToken cancellationToken)
        {
            var su = 0;
            var result= new Result() { Code=(int)CommonErrorCodes.Success};
            try
            {

                string sql = "select c.MainplanNum, b.TasksCode,c.MainplanName,b.TasksState,b.ProcessRouteNo,e.MaterialSpecsName,d.MainplandetailNum from WorkOrderBoards a inner join ProductionTasks b on a.TasksId=b.TasksId join mainpalans c on a.MainplanId=c.MainplanId join mainplandetails d on c.MainplanNum=d.MainplanNum join MaterialSpecs e on b.MaterialSpecsId=e.MaterialSpecsId";
                var list = workOrderBoardsRepository.GetAll(sql);
                //var sql = "select * from WorkOrderBoards";
                //var sq2 = "select * from ProductionTasks";
                //var sq3 = "select * from mainpalans";
                //var sq4 = "select * from mainplandetails";
                //var list = from a in workOrderBoardsRepository.GetAll(sql)
                //           join b in productionTasksRepository.GetAll(sq2)
                //           on a.TasksId equals b.TasksId
                //           join c in mainpalanRepository.GetAll(sq3)
                //           on a.MainplanId equals c.MainplanId
                //           join d in mainplandetailRepository.GetAll(sq4)
                //           on c.MainplanNum equals d.MainplanNum
                //           select new
                //           {
                //               c.MainplanNum,
                //               b.TasksCode,
                //               b.TasksName,
                //               b.TasksState,
                //               b.ProcessRouteNo,
                //               b.MaterialSpecsId,
                //               d.MainplandetailNum,
                //               su
                //           };

                result.Data = list;
            }
            catch (Exception)
            {

                throw;
            }
            return result;
        }
    }
}
